/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import entities.CategoryFoods;
import entities.Customers;
import entities.Foods;
import entities.OderFood;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author TANGQUOCTUAN
 */
@Stateless
public class FoodsFacade extends AbstractFacade<Foods> implements FoodsFacadeLocal {

    @PersistenceContext(unitName = "eProject_Tunga_Restaurant-ejbPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public FoodsFacade() {
        super(Foods.class);
    }

    public List detailFood(Integer foodid) {
        Query r = em.createNamedQuery("Foods.findByFoodID");
        r.setParameter("foodID", foodid);
        return r.getResultList();
    }

    public List getCaFoodID(Integer caFoodID) {
        CategoryFoods fff = em.find(CategoryFoods.class, caFoodID);
        Query r = em.createQuery("select f from Foods f WHERE categoryFoods.caFoodID = :CaFoodID");
        r.setParameter("CaFoodID", caFoodID);
        return r.getResultList();
    }

    public List getAllFood() {
        Query r = em.createNamedQuery("Foods.findAll");
        return r.getResultList();
    }

    public boolean updateFood(Integer id, String name, int cate, String desc, Double price, String picture) {
        Foods f = em.find(Foods.class, id);
        CategoryFoods c = em.find(CategoryFoods.class, cate);
        if (f == null) {
            return false;
        }
        f.setFoodName(name);
        f.setCategoryFoods(c);
        f.setDescription(desc);
        f.setPrice(price);
        f.setPicture(picture);
        em.persist(f);
        return true;
    }

    public boolean insertFood(String name, int cate, String des, Double price, String picture) {
        CategoryFoods c = em.find(CategoryFoods.class, cate);
        Foods f = new Foods(name, des, price, picture);
        f.setCategoryFoods(c);
        c.getFoodsList().add(f);
        em.persist(f);
        return true;
    }

    public boolean deleteFood(Integer id) {
        Foods f = em.find(Foods.class, id);
        em.remove(f);
        return true;
    }

    
}
